<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>
  
<script>
import * as echarts from 'echarts';
import { getIndexDataList } from '@/api/index'
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
    },
    height: {
      type: String,
      default: '550px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: false
    }
  },
  data() {
    return {
      chart: null,
      XData: [],
      YData: [],
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    getIndexDataList().then((response) => {
      response.userDataCountVos.forEach((v) => {
        this.XData.push(v.date)
        this.YData.push(v.count)
      })
      this.$nextTick(() => {
        this.initChart();
      });
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        title: [
          {
            top: '3%',
            left: '3%',
            text: '小程序访问人数'
          },
        ],
        tooltip: {
          trigger: 'item'
        },
        xAxis: {
          type: 'category',
          data: this.XData
        },
        yAxis: {
          type: 'value',
          interval: 10,
        },
        series: [
          {
            data: this.YData,
            type: 'line',
            smooth: true
          }
        ]
      })
    }
  }
}
</script>
  